% 07/12/11 Vasyl MYKHALCHUK
%

% ====================================================================
% In general it works in the following way:
% (1) load animated mesh from .anm file
% (2) for each correspondent triangles in sequential frames put them in 
%     the same plane and compute principal strain vector
% ====================================================================
% Given N_f - number of frames in sequence, N_t - number of triangles in
% the mesh
%
% OUTPUT:
% matrix N_t by (3 * N_f)
% ====================================================================

function [ principalStrains ] = getStrainsForAniMesh( aniMeshFileName )
% Load animated mesh
[geometryForEachFrame, topologyInfo] = loadANM(aniMeshFileName);

triangleIndex = 356;
tr = topologyInfo(triangleIndex, :);

% tr = (v1, v2, v3)

sliceForOneVertOneCoord = geometryForEachFrame(:,1,1);
Nf = length(sliceForOneVertOneCoord) - 1;


for i = 1:Nf
    v1 = geometryForEachFrame(i, 1, (tr(1)-1)*3 + 1: (tr(1)-1)*3 + 3);
    v2 = geometryForEachFrame(i, 1, (tr(2)-1)*3 + 1: (tr(2)-1)*3 + 3);
    v3 = geometryForEachFrame(i, 1, (tr(3)-1)*3 + 1: (tr(3)-1)*3 + 3);
    
    vv1 = geometryForEachFrame(i + 1, 1, (tr(1)-1)*3 + 1 : (tr(1)-1)*3 + 3);
    vv2 = geometryForEachFrame(i + 1, 1, (tr(2)-1)*3 + 1 : (tr(2)-1)*3 + 3);
    vv3 = geometryForEachFrame(i + 1, 1, (tr(3)-1)*3 + 1 : (tr(3)-1)*3 + 3);
    
    %flatten triangles (technically we affect vertices)
    [v1f, v2f, v3f] = fitTrianglesToOnePlane(v1, v2, v3);
    [vv1f, vv2f, vv3f] = fitTrianglesToOnePlane(vv1, vv2, vv3);
    
    principalStrains(i, :) = tri2D_strain(v1f, v2f, v3f, vv1f, vv2f, vv3f);
end

scatter(principalStrains(:,1),principalStrains(:,2));

end

